package cn.signalpha.modules.base.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.base.entity.Protocol;
import cn.signalpha.modules.base.service.ProtocolService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/protocol")
@Api(value = "政策协议", tags = "政策协议")
public class ProtocolController extends BaseController {

	private final ProtocolService protocolService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<Protocol>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<Protocol> queryWrapper = Condition.getQueryWrapper(params, Protocol.class);
        return R.data(protocolService.page(Condition.getPage(query), queryWrapper.lambda().orderByDesc(Protocol::getId)));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Protocol> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(protocolService.getById(id));
	}

    @PostMapping("/submit")
    @ApiOperation(value = "提交", notes = "传入protocol")
    public R submit(@Valid @RequestBody Protocol protocol) {
		protocolService.removeCaChe();
        return R.status(protocolService.saveOrUpdate(protocol));
    }

	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		protocolService.removeCaChe();
		return R.status(protocolService.deleteLogic(Func.toLongList(ids)));
	}

}
